package net.xdclass.rbac_shiro;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;

public class QuickStartTest5_3 {


    @Test
    public void testAuthentication(){
        //创建SecurityManager工厂，通过配置文件ini创建
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:jdbcrealm.ini");
        SecurityManager securityManager =factory.getInstance();
        //将securityManager 设置到当前环境中
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject =SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jack","123");
        subject.login(usernamePasswordToken);

        System.out.println("认证结果:"+subject.isAuthenticated());
        //是否有对应的角色
        System.out.println("是否有对应root1角色:"+subject.hasRole("root1"));
        //获取subject名
        System.out.println("获取subject名:"+subject.getPrincipal());
        //检查是否有对应的角色
        System.out.println("检查是否有对应的admin角色:"+subject.hasRole("admin"));
        //检查是否有对应的角色权限
        System.out.println("检查是否有对应的video:find角色:"+subject.isPermitted("video:find"));
        //退出登录
        subject.logout();
        System.out.println("认证结果:"+subject.isAuthenticated());
    }

    @Test
    public void test(){
        DefaultSecurityManager securityManager = new DefaultSecurityManager();

        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds.setUrl("jdbc:mysql://127.0.0.1:3306/xdclass_shiro?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false");
        ds.setUsername("root");
        ds.setPassword("123456");

        JdbcRealm jdbcRealm = new JdbcRealm();
        jdbcRealm.setPermissionsLookupEnabled(true);
        jdbcRealm.setDataSource(ds);

        securityManager.setRealm(jdbcRealm);

        //将securityManager 设置到当前环境中
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject =SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jack","123");
        subject.login(usernamePasswordToken);

        System.out.println("认证结果:"+subject.isAuthenticated());
        //是否有对应的角色
        System.out.println("是否有对应root1角色:"+subject.hasRole("root1"));
        //获取subject名
        System.out.println("获取subject名:"+subject.getPrincipal());
        //检查是否有对应的角色
        System.out.println("检查是否有对应的admin角色:"+subject.hasRole("admin"));
        //检查是否有对应的角色权限
        System.out.println("检查是否有对应的video:find角色:"+subject.isPermitted("video:find"));
        //退出登录
        subject.logout();
        System.out.println("认证结果:"+subject.isAuthenticated());
    }
}
